<template>
  <div>
    <transition name="showHeader">
      <div class="header" v-show="isShowHeader">
        <div v-show="$store.state.centerData.length">
          <div class="uname" @click="$router.push('/home/about')">
            {{$store.state.centerData[0] ? $store.state.centerData[0].uname :''}}
          </div>
        </div>
        <div v-show="$store.state.centerData.length" class="headerRight">
          <router-link to="/home/search" class="link">
            <el-icon :size="15">
              <Search />
            </el-icon>
            搜 索
          </router-link>
          <router-link to="/home/main" class="link">
            <el-icon :size="15">
              <HomeFilled />
            </el-icon>
            首 页
          </router-link>
          <router-link to="/home/classify" class="link">
            <el-icon :size="15">
              <Menu />
            </el-icon>
            分 类
          </router-link>
          <router-link to="/talk" class="link">
            <el-icon :size="15">
              <Position />
            </el-icon>
            动 态
          </router-link>
          <router-link to="/home/about" class="link">
            <el-icon :size="15">
              <Avatar />
            </el-icon>
            关 于
          </router-link>
          <router-link to="/message" class="link">
            <el-icon :size="15">
              <Avatar />
            </el-icon>
            留言
          </router-link>
        </div>
      </div>
    </transition>
  </div>
</template>
<script>
import { useStore } from 'vuex'
import { onMounted, reactive, toRefs, onUnmounted } from 'vue'
export default {
  name: 'HomeHeader',
  setup (props) {
    const data = reactive({
      isShowHeader: true
    })
    const store = useStore()
    window.addEventListener('scroll', scroll)
    function scroll () {
      let domScrollTop = document.documentElement.scrollTop || document.body.scrollTop
      data.isShowHeader = domScrollTop > 300 ? false : true;
    }
    onMounted(() => {
      store.dispatch('getCenterData')
    })
    onUnmounted(() => {
      window.removeEventListener('scroll', scroll)
    })
    return {
      ...toRefs(data)
    }
  }
}
</script>
<style lang="scss" scoped>
.showHeader-leave-active {
  animation: move 0.5s;
}
.showHeader-enter-active {
  animation: move 0.5s reverse;
}

@keyframes move {
  0% {
    transform: translateY(0);
    opacity: 1;
  }
  50% {
    background: #fff;
    color: #333;
  }
  100% {
    background: #fff;
    color: #333;
    opacity: 0;
    transform: translateY(-100%);
  }
}
.uname {
  font-weight: 700;
  font-size: 20px;
}
.header {
  position: fixed;
  display: flex;
  z-index: 9;
  top: 0;
  left: 0;
  width: 100%;
  height: 50px;
  background: transparent;
  color: #fff;
  justify-content: space-between;
  padding: 0 30px;
  align-items: center;

  .link {
    position: relative;
    width: 60px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 0 10px;
    color: #fff;
    text-decoration: none;
    font-weight: 700;
    &::after {
      position: absolute;
      bottom: -5px;
      left: 0;
      content: "";
      width: 0px;
      height: 2px;
      background: #74b6e2;
      transition: all 0.5s;
    }
    &:hover::after {
      width: 60px;
    }
  }
}
.headerRight {
  display: flex;
}
</style>